//
//  CollectionShowView.m
//  MiBaoTu
//
//  Created by admin on 16/9/22.
//  Copyright © 2016年 com.caesartech15. All rights reserved.
//

#import "CollectionShowView.h"

@interface CollectionShowView () {
    UILabel             *title;
    UIImageView         *_userImgView;
}

@property (nonatomic,strong)NSString    *titleName;

@end
@implementation CollectionShowView

//+(CollectionShowView *)standDefult
//{
//    static CollectionShowView *showView = nil;
//    static dispatch_once_t onceToken;
//    dispatch_once(&onceToken, ^{
//        showView = [CollectionShowView new];
//    });
//    return showView;
//}

- (id)initWithTitle:(NSString *)titleString
{
    self = [super init];
    if (self) {
        self.titleName = titleString;
        [self onCreateUI];
    }
    return self;
}

- (void)onCreateUI
{
    self.backgroundColor = TINK_COLOR;//[UIColor colorWithRed:239/255.0 green:82/255.0 blue:167/255.0 alpha:1.];
    self.frame = CGRectMake(0, 0, SCREEN_WIDTH, 0);
    
    UIImage *img = [UIImage imageNamed:@"gouxuan_fb"];
    _userImgView = [[UIImageView alloc]initWithFrame:CGRectMake(25*PERCEN, (ISIPAD?20+(44-img.size.height)/2.:(ISPortrait?20+(44-img.size.height)/2.:(36-img.size.height)/2.)), img.size.width, img.size.height)];
    _userImgView.backgroundColor = [UIColor clearColor];
    _userImgView.layer.cornerRadius = _userImgView.height / 2.;
    _userImgView.layer.masksToBounds = YES;
    _userImgView.image = img;
    [self addSubview:_userImgView];
    
    title = [[UILabel alloc]initWithFrame:CGRectMake(_userImgView.right+5, (ISIPAD?20+12:(ISPortrait?20+12:8)), 200, 20)];
    title.backgroundColor = [UIColor clearColor];
    title.textAlignment = NSTextAlignmentLeft;
    title.textColor = [UIColor whiteColor];
    title.font = [UIFont systemFontOfSize:14.f];
    title.text = [NSString stringWithFormat:@"已经添加到分类:%@！",(self.titleName.length == 0 ? @"收藏":self.titleName)];
    [self addSubview:title];
    
    if (ISIPAD) {
        float itemWidth = [title.text boundingRectWithSize:CGSizeMake(1000, title.height) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:title.font} context:nil].size.width+10;
        title.frame = CGRectMake((SCREEN_WIDTH-itemWidth)/2., title.top, itemWidth, 20);
        _userImgView.right = title.left-20*PERCEN;
    }
}

- (void)setOrientation:(UIDeviceOrientation)orientation
{
    _orientation = orientation;
    if (self.superview) {
        self.width = SCREEN_WIDTH;
        
        if (ISIPAD) {
            float itemWidth = [title.text boundingRectWithSize:CGSizeMake(1000, title.height) options:NSStringDrawingUsesLineFragmentOrigin|NSStringDrawingUsesFontLeading attributes:@{NSFontAttributeName:title.font} context:nil].size.width+10;
            title.frame = CGRectMake((SCREEN_WIDTH-itemWidth)/2., title.top, itemWidth, 20);
            _userImgView.right = title.left-20*PERCEN;
        }
    }
}

- (void)showInView
{
    [[UIApplication sharedApplication].keyWindow addSubview:self];
    [UIView animateWithDuration:.35 animations:^{
        self.height = (ISIPAD?NAV_HEIGHT:(ISPortrait?NAV_HEIGHT:36));
    }];
    [self performSelector:@selector(removeForWindon) withObject:nil afterDelay:2];
}

- (void)removeForWindon
{
    [UIView animateWithDuration:.35 animations:^{
        self.height = 0;
        _userImgView.hidden = YES;
        title.hidden = YES;
    } completion:^(BOOL finished) {
        [self removeFromSuperview];
    }];
}

@end
